package q12_intToRoman;

public class Solution {
    public static void main(String[] args) {

        System.out.println(intToRoman(58));// "LVIII"
        System.out.println(intToRoman(1994));// "MCMXCIV"

    }

    public static String intToRoman(int num) {
        StringBuilder res = new StringBuilder();
        int mode = 0,bin = 0;
        while (num > 0) {
            bin = num%10;
            res.insert(0,charSum(bin, mode));
            mode += 1;
            num = num / 10;
        }

        return res.toString();
    }



    private static String charSum(int num,int mode){
        String c1 = "",c2 = "",c3 = "";
        StringBuilder res = new StringBuilder();
        if(mode == 0){
            c1 = "I";
            c2 = "V";
            c3 = "X";
        }else if(mode == 1){
            c1 = "X";
            c2 = "L";
            c3 = "C";
        }else if(mode == 2){
            c1 = "C";
            c2 = "D";
            c3 = "M";
        }else if(mode == 3){
            c1 = "M";
        }


        if(num < 4){
            // res.append(String.valueOf(c).repeat(Math.max(0, num)));
            res.append(c1.repeat(num));
        }else if (num == 4) {
            res.append(c1).append(c2);
        }else if (num < 9) {
            res.append(c2).append(c1.repeat(num - 5));
        }else if (num == 9) {
            res.append(c1).append(c3);
        }

        return res.toString();
    }

}
